package com.hfb.mashibing.alip8.designpattern._09observer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 题目：模拟 宝宝在睡觉，醒来后哭了，之后要喂奶
 * 面向对象的傻等，直到醒来
 */

public class V5 {

    public static void main(String[] args) throws InterruptedException {
        ChildV5 baby = new ChildV5();
        TimeUnit.SECONDS.sleep(3);
        // do something
        baby.wakeUp();

    }
}

/**
 * 加入多个观察者
 */
class ChildV5 {
    private boolean cry = false;
    /*观察者与被观察者分离*/
    private List<Observer> observers = new ArrayList<Observer>(){{
        add(new MamV5());
        add(new DadV5());
        add(new NaiNaiV5());
    }};
    public ChildV5() {
        System.out.println("baby is doing sleepping !");
    }

    public boolean isCry() { return cry;}

    public void wakeUp(){
        cry = true;
        System.out.println("waked up! Crying! 555555555555555555");
        observers.forEach(
            o -> o.actionOnWakeUp()
        );
    }
}

/**
 * 加入观察者
 */
interface Observer {
    void actionOnWakeUp();
}
class DadV5 implements Observer {
    public void actionOnWakeUp() {
        System.out.println("he is Crying! dad is go to say !");
    }
}

class MamV5 implements Observer {
    public void actionOnWakeUp() {
        System.out.println("he is Crying! MamV5 is go to hug !");
    }
}

class NaiNaiV5 implements Observer {
    public void actionOnWakeUp() {
        System.out.println("he is Crying! NaiNaiV5 is go to feed !");
    }
}
